//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Utils/Base/Common.h"

namespace Engine
{
namespace Utils
{

	//
	// Utils Core
	//

	class UtilsCore
	{
	// variables
	private:
		CLogger				_logger;
		CConfig				_config;
		string				_resPath;


	// methods
	public:
		UtilsCore () {}

		~UtilsCore () {}

		bool Init (uni_c_string resPath, uni_c_string log, bool debugConfig)
		{
			CHECK_ERR( FileSystem::FindAndSetCurrentDir( resPath.cstr(), 6 ) );
		
			CHECK_ERR(	_logger.Create(	e_log_output::MESSAGE_BIT
										  |	e_log_output::LOG_FILE_BIT
										  |	e_log_output::CONSOLE_BIT
										  |	e_log_output::IDE_CONSOLE_BIT
										  , log.cstr() ) );
			
			_config.SaveParsedFiles( debugConfig );

			if ( debugConfig )
			{
				const uni_c_string	path( "_debug" );

				if ( not FileSystem::IsDirectoryExist( path.cstr() ) )
					FileSystem::NewDirectory( path.cstr() );

				_config.SetParsedFilesDirectory( path.cstr() );
			}

			_resPath = resPath;
			return true;
		}

		bool Parse (uni_c_string filename, const static_obj_tree_t &cfgTree)
		{
			CConfigContainerPtr		container;

			CHECK_ERR( _config.Parse( filename.cstr(), container.ref() ) );
			
			CHECK_ERR( container->Process( cfgTree.First() ) );
			return true;
		}

		bool ClearDir (uni_c_string dir)
		{
			string_array_t	files;

			CHECK_ERR( FileSystem::GetAllFilesInPath( dir.cstr(), files ) );

			FOR( i, files )
			{
				string path;
				FileAddressUtils::BuildPath( path, dir, files[i] );

				CHECK( FileSystem::DeleteFile( path.cstr() ) );
			}

			return true;
		}

		CConfig * Config ()		{ return &_config; }

		string const &	GetResourcePath ()	const	{ return _resPath; }
	};

}	// Utils
}	// Engine
